package com.example.clientdemo.service;

import cn.hutool.http.HttpRequest;
import com.example.clientdemo.Utils.XStreamXmlBeanUtil;
import com.example.clientdemo.pojo.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WeatherService {

    /**
     * 获得国外国家名称和与之对应的ID
     */

    public List<String> getRegionCountry() {
        // 1、构建xml参数
        /**
         * <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
         *    <soapenv:Header/>
         *    <soapenv:Body>
         *       <web:getRegionCountry/>
         *    </soapenv:Body>
         * </soapenv:Envelope>
         */
        SoapenvEnvelope soapenvEnvelope = new SoapenvEnvelope();
        soapenvEnvelope.setXmlnsSoapenv("http://schemas.xmlsoap.org/soap/envelope/");
        soapenvEnvelope.setXmlnsWeb("http://WebXml.com.cn/");
        soapenvEnvelope.setSoapenvHeader("");
        SoapenvBody soapenvBody = new SoapenvBody();
        soapenvBody.setWebGetRegionCountry("");
        soapenvEnvelope.setSoapenvBody(soapenvBody);


        String request = XStreamXmlBeanUtil.toXml(soapenvEnvelope);
        System.out.println(request);
        // 2、调用post请求，请求体为xml
        String response = HttpRequest.post("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl")
                .body(request)
                .header("Content-Type", "text/xml; charset=utf-8")
                .execute()
                .body();
        System.out.println(response);
        // 3、响应解析
        /**
         * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *    <soap:Body>
         *       <getRegionCountryResponse xmlns="http://WebXml.com.cn/">
         *          <getRegionCountryResult>
         *             <string>阿尔及利亚,3320</string>
         *             <string>阿根廷,3522</string>
         *             <string>阿曼,3170</string>
         *             <string>阿塞拜疆,3176</string>
         *          </getRegionCountryResult>
         *       </getRegionCountryResponse>
         *    </soap:Body>
         * </soap:Envelope>
         */
        SoapenvEnvelopeResp soapenvEnvelopeResp = XStreamXmlBeanUtil.fromXml(response, SoapenvEnvelopeResp.class);
        System.out.println(soapenvEnvelopeResp);
        return soapenvEnvelopeResp.getSoapBody().getRegionCountryResponse().getRegionCountryResult().getList();
    }

    /**
     * 获得支持的城市/地区名称和与之对应的ID
     */
    public List<City> getSupportCityDataset(String regionCode) {
        // 1、构建xml参数
        /**
         * <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
         *    <soapenv:Header/>
         *    <soapenv:Body>
         *       <web:getSupportCityDataset>
         *          <!--Optional:-->
         *          <web:theRegionCode>广东</web:theRegionCode>
         *       </web:getSupportCityDataset>
         *    </soapenv:Body>
         * </soapenv:Envelope>
         */
        SoapenvEnvelope soapenvEnvelope = new SoapenvEnvelope();
        soapenvEnvelope.setXmlnsSoapenv("http://schemas.xmlsoap.org/soap/envelope/");
        soapenvEnvelope.setXmlnsWeb("http://WebXml.com.cn/");
        soapenvEnvelope.setSoapenvHeader("");
        SoapenvBody soapenvBody = new SoapenvBody();
        WebGetSupportCityDataset webGetSupportCityDataset = new WebGetSupportCityDataset();
        if(regionCode != null && regionCode.length() > 0) {
            webGetSupportCityDataset.setWebTheRegionCode(regionCode);
        } else {
            webGetSupportCityDataset.setWebTheRegionCode("");
        }
        soapenvBody.setWebGetSupportCityDataset(webGetSupportCityDataset);
        soapenvEnvelope.setSoapenvBody(soapenvBody);
        String request = XStreamXmlBeanUtil.toXml(soapenvEnvelope);
        // 2、调用post请求，请求体为xml
        String response = HttpRequest.post("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl")
                .body(request)
                // text/xml;charset=utf-8
                .header("Content-Type", "text/xml; charset=utf-8")
                .execute()
                .body();
        // 3、响应解析
        /**
         * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
         *    <soap:Body>
         *       <getSupportCityDatasetResponse xmlns="http://WebXml.com.cn/">
         *          <getSupportCityDatasetResult>
         *             <xs:schema id="Region" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
         *                <xs:element name="Region" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
         *                   <xs:complexType>
         *                      <xs:choice minOccurs="0" maxOccurs="unbounded">
         *                         <xs:element name="City">
         *                            <xs:complexType>
         *                               <xs:sequence>
         *                                  <xs:element name="CityID" type="xs:int" minOccurs="0"/>
         *                                  <xs:element name="CityName" type="xs:string" minOccurs="0"/>
         *                               </xs:sequence>
         *                            </xs:complexType>
         *                         </xs:element>
         *                      </xs:choice>
         *                   </xs:complexType>
         *                </xs:element>
         *             </xs:schema>
         *             <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
         *                <Region xmlns="">
         *                   <City diffgr:id="City1" msdata:rowOrder="0">
         *                      <CityID>2355</CityID>
         *                      <CityName>博罗</CityName>
         *                   </City>
         *                   <City diffgr:id="City2" msdata:rowOrder="1">
         *                      <CityID>2367</CityID>
         *                      <CityName>潮阳</CityName>
         *                   </City>
         *                </Region>
         *             </diffgr:diffgram>
         *          </getSupportCityDatasetResult>
         *       </getSupportCityDatasetResponse>
         *    </soap:Body>
         * </soap:Envelope>
         */
        SoapenvEnvelopeResp soapenvEnvelopeResp = XStreamXmlBeanUtil.fromXml(response, SoapenvEnvelopeResp.class);
        Region region = soapenvEnvelopeResp.getSoapBody().getSupportCityDatasetResponse().getSupportCityDatasetResult().getDiffgrDiffgram().getRegion();
        return region == null ? null : region.getCityList();
    }
}
